package com.cloudspace.jindun.activity.xiaoxi;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.HuaTiListAdapter;
import com.cloudspace.jindun.model.Topic;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import okhttp3.Call;

public class HistoricalActivity extends CustomTitleBaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.huati_title_back_lishihuati)
    ImageView mHuatiTitleBackLishihuati;
    @Bind(R.id.huti_lv_lsht)
    ListView mHutiLvLsht;
    @Bind(R.id.huti_swipe_lsht)
    SwipeRefreshLayout mHutiSwipeLsht;

    private HuaTiListAdapter mHuaTiListAdapter;
    private Topic mTopic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xiaoxi_huati_lishihuati, false);
        ButterKnife.bind(this);
        initData();
        setListener();
    }

    @OnClick(R.id.huati_title_back_lishihuati)
    public void onClick() {
        finish();
    }

    private void setListener() {
        mHutiLvLsht.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mTopic != null && mTopic.getData() != null && mTopic.getData().size() > 0) {
                    RongIM.getInstance().startGroupChat(HistoricalActivity.this, mTopic.getData().get(position).getChannelId(), mTopic.getData().get(position).getChannelName());
                }
            }
        });
        mHutiSwipeLsht.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        if (mTopic != null) {
            mHuaTiListAdapter.clear();
        }
        requestData(YXHttpUrl.XX_HQHT, UserSessionManager.getInstance().getUserId());
    }

    private void initData() {
        mHuaTiListAdapter = new HuaTiListAdapter(this);
        mHutiLvLsht.setAdapter(mHuaTiListAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mTopic != null) {
            mHuaTiListAdapter.clear();
        }
        requestData(YXHttpUrl.XX_HQHT, UserSessionManager.getInstance().getUserId());
    }

    private void requestData(String url, final String reportId) {
        OkHttpUtils.post()
                .url(url)
                .addParams("userId", reportId)
                .addParams("Status", "0002")
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mHutiSwipeLsht.setRefreshing(false);
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        if (response != null) {
                            Gson gson = new Gson();
                            mTopic = gson.fromJson(response, Topic.class);
                            mHuaTiListAdapter.addAll(mTopic.getData());
                            mHutiSwipeLsht.setRefreshing(false);
                        }

                    }
                });
    }
}
